<?php
declare(strict_types=1);

namespace app\admin\business;

use app\common\model\FeedbackContent as FeedbackContentModel;

class FeedbackContent extends AdminBase
{
    private $_feedbackContentObj = null;

    public function __construct(string $websiteCode, $name)
    {
        $this->_websiteCode        = $websiteCode;
        $this->_feedbackContentObj = new FeedbackContentModel([], $websiteCode, 'feedback_' . $name);
    }

    public function getFeedbackContentList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = null;
        $count = 0;
        try {
            $rows  = $this->_feedbackContentObj->getFeedbackContentList($page, $limit, $where);
            $count = $this->_feedbackContentObj->getFeedbackContentCount($where);
        } catch (\Exception $e) {
            logErr('读取自定义表单的留言列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function findFeedbackContentById(int $id, int $formId): array
    {
        $feedbackContent = [];
        try {
            $feedbackContent = $this->_feedbackContentObj->findFeedbackContentById($id, $formId);
        } catch (\Exception $e) {
            logErr('通过主键读取自定义表单的留言详情失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($feedbackContent)) {
            return [];
        }

        return $feedbackContent;
    }

    public function updateFeedbackContent(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_feedbackContentObj->updateFeedbackContent($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新自定义表单的留言信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteFeedbackContent(int $id, int $formId): array
    {
        try {
            return [self::_RESULT => $this->_feedbackContentObj->deleteFeedbackContent($id, $formId), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除自定义表单的留言信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchDeleteFeedbackContent(array $ids, int $formId): array
    {
        try {
            return [self::_RESULT => $this->_feedbackContentObj->batchDeleteFeedbackContent($ids, $formId), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键批量删除自定义表单的留言信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchDoCheck(array $data,  int $status): array
    {
        try {
            return [self::_RESULT => $this->_feedbackContentObj->batchDoCheck($data,  $status), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键批量审核自定义表单的留言信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}

